

#### Uganda Results #### 
packs <- c('dplyr', 'lfe', 'interflex', 'ggplot2', 'ggthemes')
lapply(packs, require, character.only = T)
setwd('~/Dropbox/public_health_repression/replication-for-IS/')
theme_set(theme_tufte())

#### Event Study ##### 
uga_df <- readRDS('data/uga_df.rds')

m <- felm(repress_bin ~ opp:l6 + opp:l5 + opp:l4 + opp:l3 + opp:l2 + 
            opp:p + opp:p1 + opp:p2 + opp:p3 | factor(grid_id) + event_time  |0| grid_id, 
          data = subset(uga_df, period > 20))
summary(m)

y <- c(m$coefficients[1:5], 0, m$coefficients[6:9])
coef.name <- c(seq(-6, 3, by =1))
plot.df <- cbind.data.frame(y, coef.name)
plot.df$se <- c(summary(m)$coefficients[1:5,2], 0, summary(m)$coefficients[6:9,2])
plot.df$u <- plot.df$y + plot.df$se*1.96
plot.df$l <- plot.df$y - plot.df$se*1.96

m <- felm(repress_bin ~ opp:lockdown| factor(grid_id) + event_time   |0| grid_id, 
          data = subset(uga_df, period > 20))
summary(m)

p1 <- ggplot(plot.df, aes(x = coef.name, y = y, group = 1)) +
  geom_point(size = 2) +
  geom_line() +
  geom_errorbar(aes(ymax = u, ymin = l), width=.7,
                position=position_dodge(.06), size = .3) +
  geom_hline(yintercept = 0) +
  geom_rect(aes(xmin=0, xmax=Inf, ymin=-Inf, ymax=Inf), alpha = .01004) +
  geom_hline(yintercept = 0) +
  geom_vline(xintercept = -1, colour="#BB0000", linetype="dashed") +
  ggtitle("DD") +
  annotate("label", x = 1, y = Inf, label = "Post-Lockdown",
           hjust=1, vjust=1, family="Times") +
  annotate("label", x = -1, y = Inf, label = "Pre-Lockdown",
           hjust=1, vjust=1, family="Times") +
  annotate("label", x = 0, y = -Inf, label = "Lockdown",
           hjust=.5, vjust=0, family="Times") +
  annotate("label", x = Inf, y = Inf, label = "ATT: 0.36*** \nSt.Dev(Opposition):0.15",
           hjust=1, vjust=1, family="Times") +
  xlab("Months from Lockdown") +
  ylab("DD Estimate (Repression)") +
  ggtitle("State Violence in Opposition Areas") + 
  theme_tufte()
p1

setwd('~/Dropbox/public_health_repression/replication-for-IS/figures')
ggsave("event_main_out.png", p1, width = 13)


#### Covariates #### 


#### Covariate Adjustment #### 
m1 <- felm(repress_bin ~ opp:lockdown | factor(grid_id) + event_time   |0| grid_id, 
           data = subset(uga_df, period > 20))
m2 <- felm(repress_bin ~ opp:lockdown | factor(grid_id) + event_time + 
             ethfrac:event_time + pop_den:event_time + gdpdat:event_time  |0| grid_id, 
           data = subset(uga_df, period > 20))
m3 <- felm(repress_bin ~ opp:lockdown | factor(grid_id) + event_time + 
             ethfrac:event_time + pop_den:event_time + gdpdat:event_time + factor(REGION):event_time  |0| grid_id, 
           data = subset(uga_df, period > 20))


model1Frame <- data.frame(Variable = 'Shutdown x Opposition',
                          Coefficient = m1$beta[1],
                          SE = m1$cse[1],
                          Models = "Baseline")
model2Frame <- data.frame(Variable = 'Shutdown x Opposition',
                          Coefficient = m2$beta[1],
                          SE = m2$cse[1],
                          Models = "Covariates")
model3Frame <- data.frame(Variable = 'Shutdown x Opposition',
                          Coefficient = m3$beta[1],
                          SE = m3$cse[1],
                          Models = "Covariates + Region x Time FE")
allModelFrame <- data.frame(rbind(model1Frame, model2Frame, model3Frame)) 

interval1 <- -qnorm((1-0.9)/2)  
interval2 <- -qnorm((1-0.95)/2)  

zp1 <- ggplot(allModelFrame, aes(shape = Models))
zp1 <- zp1 + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
zp1 <- zp1 + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*interval1,
                                ymax = Coefficient + SE*interval1),
                            lwd = 1, position = position_dodge(width = 1/2))
zp1 <- zp1 + geom_pointrange(aes(x = Variable, y = Coefficient, ymin = Coefficient - SE*interval2,
                                 ymax = Coefficient + SE*interval2),
                             lwd = 1/2, position = position_dodge(width = 1/2), fill = "WHITE")
zp1 <- zp1 + theme_tufte()
zp1 <- zp1 + ggtitle("Repression (Binary)")  + xlab("") + ylab("Effect of Lockdown on Opposition Areas") + 
  scale_fill_discrete(name = 'Models') + theme(legend.position = "bottom")
zp1

setwd('~/Dropbox/public_health_repression/replication-for-IS/figures')
ggsave("covariates_out.png", zp1, width = 13)

#### Protests and Riots #### 

m1 <- felm(protest_bin ~ opp:lockdown | factor(grid_id) + event_time  |0| grid_id, 
           data = subset(uga_df, period > 20))
m2 <- felm(protest_bin ~ opp:lockdown | factor(grid_id) + event_time + 
             ethfrac:event_time + pop_den:event_time + gdpdat:event_time  |0| grid_id, 
           data = subset(uga_df, period > 20))
m3 <- felm(protest_bin ~ opp:lockdown | factor(grid_id) + event_time + 
             ethfrac:event_time + pop_den:event_time + gdpdat:event_time + factor(REGION):event_time  |0| grid_id, 
           data = subset(uga_df, period > 20))

model1Frame <- data.frame(Variable = 'Shutdown x Opposition',
                          Coefficient = m1$beta[1],
                          SE = m1$cse[1],
                          Models = "Baseline")
model2Frame <- data.frame(Variable = 'Shutdown x Opposition',
                          Coefficient = m2$beta[1],
                          SE = m2$cse[1],
                          Models = "Covariates")
model3Frame <- data.frame(Variable = 'Shutdown x Opposition',
                          Coefficient = m3$beta[1],
                          SE = m3$cse[1],
                          Models = "Covariates + Region x Time FE")
allModelFrame <- data.frame(rbind(model1Frame, model2Frame, model3Frame))  # etc.

interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier

# Plot
zp1 <- ggplot(allModelFrame, aes(shape = Models))
zp1 <- zp1 + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
zp1 <- zp1 + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*interval1,
                                ymax = Coefficient + SE*interval1),
                            lwd = 1, position = position_dodge(width = 1/2))
zp1 <- zp1 + geom_pointrange(aes(x = Variable, y = Coefficient, ymin = Coefficient - SE*interval2,
                                 ymax = Coefficient + SE*interval2),
                             lwd = 1/2, position = position_dodge(width = 1/2), fill = "WHITE")
zp1 <- zp1 + theme_tufte()
zp1 <- zp1 + ggtitle("Protests (Binary)")  + xlab("") + ylab("Effect of Lockdown on Opposition Areas") + 
  scale_fill_discrete(name = 'Model') + theme(legend.position = "bottom")
zp1


m1 <- felm(riot_bin ~ opp:lockdown | factor(grid_id) + event_time  |0| grid_id, 
           data = subset(uga_df, period > 20))
m2 <- felm(riot_bin ~ opp:lockdown | factor(grid_id) + event_time + 
             ethfrac:event_time + pop_den:event_time + gdpdat:event_time  |0| grid_id, 
           data = subset(uga_df, period > 20))
m3 <- felm(riot_bin ~ opp:lockdown | factor(grid_id) + event_time + 
             ethfrac:event_time + pop_den:event_time + gdpdat:event_time + factor(REGION):event_time  |0| grid_id, 
           data = subset(uga_df, period > 20))

model1Frame <- data.frame(Variable = 'Shutdown x Opposition',
                          Coefficient = m1$beta[1],
                          SE = m1$cse[1],
                          Model = "Baseline")
model2Frame <- data.frame(Variable = 'Shutdown x Opposition',
                          Coefficient = m2$beta[1],
                          SE = m2$cse[1],
                          Model = "Covariates")
model3Frame <- data.frame(Variable = 'Shutdown x Opposition',
                          Coefficient = m3$beta[1],
                          SE = m3$cse[1],
                          Model = "Covariates + Region x Time FE")
allModelFrame <- data.frame(rbind(model1Frame, model2Frame, model3Frame))  

interval1 <- -qnorm((1-0.9)/2)  
interval2 <- -qnorm((1-0.95)/2)  

zp2 <- ggplot(allModelFrame, aes(shape = Model))
zp2 <- zp2 + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
zp2 <- zp2 + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*interval1,
                                ymax = Coefficient + SE*interval1),
                            lwd = 1, position = position_dodge(width = 1/2))
zp2 <- zp2 + geom_pointrange(aes(x = Variable, y = Coefficient, ymin = Coefficient - SE*interval2,
                                 ymax = Coefficient + SE*interval2),
                             lwd = 1/2, position = position_dodge(width = 1/2), fill = "WHITE")
zp2 <- zp2 + theme_tufte()
zp2 <- zp2 + ggtitle("Riots (Binary)")  + xlab("") + ylab("Effect of Lockdown on Opposition Areas") + 
  scale_fill_discrete(name = 'Models', labels = 'Models') + theme(legend.position = "bottom")

p3 <- plot_grid(zp1, zp2, labels = 'AUTO')


setwd('~/Dropbox/public_health_repression/replication-for-IS/figures')
ggsave("placebo_out.png", p3, width = 13)


#### Interflex #### 
library(interflex)
uga_df$Lockdown <- uga_df$lockdown
out <- interflex(Y="repress_bin", D="Lockdown", X="opp", data = uga_df, estimator = 'binning', vartype = "robust", main = "Marginal Effects", theme.bw = TRUE)
p1 <- plot(out,  bin.labs = T, cex.axis = 0.8, cex.lab = 0.8, theme.bw = T, 
           Xlabel = "Opposition Vote Share", Ylabel = "Repression", main = "Heterogeneous Effect of Lockdown")
p1
ggsave("interflex.png", p1, width = 13)




